package com.kbt.ui;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.*;
import com.android.volley.Request;
import com.kbt.activity.R;
import com.kbt.commont.Constants;
import com.kbt.listener.ClickChangeGoodCountListener;
import com.kbt.listener.ClickDimsPopWindowListener;
import com.kbt.listener.ClickStartActivityListener;
import com.kbt.listener.PopWindowDismissListener;
import com.kbt.model.data.*;
import com.kbt.net.RequestManager;
import com.kbt.util.data.StaticData;
import com.kbt.util.tool.BtnSetting;
import com.kbt.util.tool.PopWindowSetting;
import com.kbt.util.utils.ImageManager;
import com.kbt.util.utils.Utils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhgk
 * @package: com.kbt.ui
 * @TODO: 点击立即购买按钮弹出的对话框
 * @data: 2015/8/11 10:59
 * @version: V1.0
 */
public class BuyNowPopWindow extends PopupWindow implements RequestManager.ResponseInterface{

    //生命周期
    private Activity context;
    //商品大图
    private ImageView goodImg;
    //关闭按钮图片
    private ImageView closeImg;
    //减号按钮
    private Button subBtn;
    //加号按钮
    private Button addBtn;
    //数量编辑
    private EditText countEdit;
    //立即购买按钮
    private Button buyBtn;
    //商品标题
    private TextView goodTitle;
    //视图信息
    private View byNowView;
    private ShopInfo goodInfo;
    private ShopCarBean goodForCarInfo;
    private List<ShopCarBean> list = new ArrayList<ShopCarBean>();

    //库存
    private List<KuCunBean> kuCunBeans = new ArrayList<KuCunBean>();
    private KuCunListBean kuCunListBean;

    private RequestManager mRequestManager;
    private Map<String, Object> map =null;

    /***
     * 购造函数
     *
     * @param context
     */
    public BuyNowPopWindow(final Activity context,final ShopInfo goodInfo) {
        //初始化信息
        super(context);
        this.context = context;
        this.goodInfo = goodInfo;
        mRequestManager = new RequestManager();
        mRequestManager.setResponseListener(this);
        map = new HashMap<String, Object>();
        //初始化ImageLoader
        ImageManager.init();

        //获取布局信息
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        byNowView = inflater.inflate(R.layout.good_buy_now_layout, null);
        goodImg = (ImageView) byNowView.findViewById(R.id.gbnl_good_img);
        ImageManager.imageLoader.displayImage(Constants.ServiceInterFace.imageURL + goodInfo.getGoods_icon(), goodImg, ImageManager.getOptions(
                R.drawable.loadings, R.drawable.loadings));
        //商品单价
        goodTitle = (TextView) byNowView.findViewById(R.id.gbnl_good_price);
        goodTitle.setText("￥"+goodInfo.getGoods_new_price()+"(库存:"+goodInfo.getStore_counts()+")");
        //关闭按钮
        closeImg = (ImageView) byNowView.findViewById(R.id.gbnl_close_img);
        ClickDimsPopWindowListener closeImgOnClickListener = new ClickDimsPopWindowListener(this);
        closeImg.setOnClickListener(closeImgOnClickListener);
        subBtn = (Button) byNowView.findViewById(R.id.gbnl_sub_btn);
        addBtn = (Button) byNowView.findViewById(R.id.gbnl_add_btn);
        countEdit = (EditText) byNowView.findViewById(R.id.gbnl_good_count);
        //立即购买按钮
        buyBtn = (Button) byNowView.findViewById(R.id.gbnl_buy_btn);
        //设置视图
        this.setContentView(byNowView);
        //设置弹出窗口样式
        PopWindowSetting.setPopWindowStyle(this);

        //设置父窗体背景
        PopWindowSetting.setParentBackGround(context, 0.3f);

        //设置窗口销毁时事件
        PopWindowDismissListener dismissListener = new PopWindowDismissListener(context);
        this.setOnDismissListener(dismissListener);

        //减按钮事件
        subBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(Integer.parseInt(countEdit.getText().toString())==1){
                    countEdit.setText("1");
                }else{
                    int count =Integer.parseInt(countEdit.getText().toString())-1;
                    countEdit.setText(count+"");
                }
            }
        });

        addBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int count = Integer.parseInt(countEdit.getText().toString())+1;
                countEdit.setText(count + "");
            }
        });

        //立即购买
        buyBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Integer.parseInt(countEdit.getText().toString()) > Integer.parseInt(goodInfo.getStore_counts())) {
                    Toast.makeText(context, "库存不足!", Toast.LENGTH_SHORT).show();
                    return;
                }

                httpKucun();
            }
        });


    }

    /***
     * 库存
     */
    private void httpKucun() {
        if(Utils.isNetworkAvailable(context)) {
            mRequestManager.requestGet(Request.Method.GET, Constants.ServiceInterFace.selectKunCunURL+ "\'"+goodInfo.getGoods_id()+"\'", Constants.RequestTag.selectKunCunTag, map, KuCunListBean.class);
        }else{
            Toast.makeText(context,R.string.Wangluo,Toast.LENGTH_SHORT).show();
        }
    }
    //获取商品信息
    private void getShopInfo(){
        list.clear();
        goodForCarInfo = new ShopCarBean();
        goodForCarInfo.setGoods_title(goodInfo.getGoods_title());
        goodForCarInfo.setGoods_count(countEdit.getText().toString());
        goodForCarInfo.setGoods_new_price(goodInfo.getGoods_new_price());
        goodForCarInfo.setGoods_icon(goodInfo.getGoods_icon());
        goodForCarInfo.setGoods_no(goodInfo.getGoods_no());
        goodForCarInfo.setGoods_id(goodInfo.getGoods_id());
        goodForCarInfo.setGoods_tm(goodInfo.getGoods_tm());
        goodForCarInfo.setGoods_price(goodInfo.getGoods_price());
        list.add(goodForCarInfo);
    }

    @Override
    public <T> void successResponse(T parsedGSON, int tag) {
        if (tag == Constants.RequestTag.selectKunCunTag) {
            /** 判断返回值不为空 **/
            if (parsedGSON != null) {
                kuCunListBean = (KuCunListBean) parsedGSON;
                if(kuCunListBean.getResult().equals("success")){
                    if(kuCunListBean.getData().size()>0){
                        kuCunBeans = kuCunListBean.getData();
                       if(Integer.parseInt(countEdit.getText().toString()) <=Integer.parseInt(kuCunBeans.get(0).getStore_counts())){
                           getShopInfo();
                           Intent intent = new Intent(context, StaticData.GOOD_FOR_PAY_CLASS);
                           intent.putExtra("list", (Serializable) list);
                           intent.putExtra("biaoji", "buyNow");
                           context.startActivity(intent);
                           context.finish();
                       }else{
                           Toast.makeText(context, "库存不足!", Toast.LENGTH_SHORT).show();
                           goodTitle.setText("￥" + goodInfo.getGoods_new_price() + "(库存:" + kuCunBeans.get(0).getStore_counts()+")");
                       }
                    }
                }
            }
        }
    }

    @Override
    public void errorResonse(String retmeg, int tag) {
        Toast.makeText(context, R.string.noWangluo, Toast.LENGTH_SHORT).show();
    }
}
